package helexis.game.tyrian.screen;

import helexis.game.tyrian.Tyrian;
import helexis.game.tyrian.service.PreferencesManager;
import helexis.game.tyrian.service.MusicManager.TyrianMusic;
import helexis.game.tyrian.service.SoundManager.TyrianSound;

import java.util.Locale;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

public class OptionsScreen extends AbstractScreen {
	
	/**
	 * 효과음 체크 박스
	 */
	private CheckBox soundEffectsCheckbox;
	/**
	 * 배경 음악 체크 박스
	 */
	private CheckBox musicCheckbox;
	/**
	 * 볼륨 슬라이더
	 */
	private Slider volumeSlider;
	/**
	 * 볼륨 텍스트
	 */
	private Label volumeText;
	
	
	/**
	 * 생성자
	 * 
	 * @param game Tyrian 객체
	 */
	public OptionsScreen(Tyrian game) {
		super(game);
	}//end of constructor()
	
	
	@Override
	public void show() {

		super.show();
		
		if (table == null) {
			this.init();
		}//end if
		
		PreferencesManager pref = game.getPreferencesManager();
		
		this.soundEffectsCheckbox.setChecked(pref.isSoundEnabled());
		this.musicCheckbox.setChecked(pref.isMusicEnabled());
		
		this.volumeSlider.setValue(pref.getVolume());
		this.updateVolume(pref.getVolume());
		
		
	}//end of show()
	
	
	/**
	 * UI 레이아웃을 만들고 이벤트를 달아 놓는 초기화 작업을 수행합니다.
	 */
	private void init() {
		
		Skin skin = getSkin();
		
		table = new Table(skin);
		table.setFillParent(true);
		stage.addActor(table);
		
		table.add("Options").colspan(3).spaceBottom(20);
		table.row();
		
		table.defaults().pad(8).spaceBottom(10);
		
		
		table.add("Sound Effects");
		
		this.soundEffectsCheckbox = new CheckBox("", skin);
		this.soundEffectsCheckbox.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				game.getPreferencesManager().setSoundEnabled(soundEffectsCheckbox.isChecked());
				game.getSoundManager().setEnabled(soundEffectsCheckbox.isChecked());
				game.getSoundManager().play(TyrianSound.CLICK);
			}//end of clicked()
		});
		
		table.add(this.soundEffectsCheckbox).colspan(2).left();
		table.row();
		
		
		table.add("Music");
		
		this.musicCheckbox = new CheckBox("", skin);
		
		this.musicCheckbox.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				boolean enabled = musicCheckbox.isChecked();
				game.getPreferencesManager().setMusicEnabled(enabled);
				game.getMusicManager().setEnabled(enabled);
				game.getSoundManager().play(TyrianSound.CLICK);
				
				if (enabled) {
					game.getMusicManager().play(TyrianMusic.MENU);
				}//end if
				
			}//end of clicked()
		});
		
		table.add(this.musicCheckbox).colspan(2).left();
		table.row();
		
		
		table.add("Volume");
		
		this.volumeSlider = new Slider(0f, 1f, 0.1f, false, skin);
		
		this.volumeText = new Label("", skin);
		
		this.volumeSlider.addListener(new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {
				float v = volumeSlider.getValue();
				volumeText.setText(Float.toString(v));
				updateVolume(v);
				game.getSoundManager().setVolume(v);
				game.getMusicManager().setVolume(v);
			}//end of changed()
		});
		
		table.add(this.volumeSlider);
		table.add(this.volumeText).width(40);
		table.row();
		
		
		TextButton backButton = new TextButton("Back", skin);
		backButton.addListener(new ClickListener() {
			
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				game.getSoundManager().play(TyrianSound.CLICK);
				game.setScreen(game.getMenuScreen());
			}//end of clicked()
			
		});
		table.add(backButton).size(300, 60).colspan(3);
		
	}//end of init()
	
	
	
	/**
	 * 주어진 볼륨 정보로 게임 내 볼륨 설정 정보 및 화면 텍스트를 업데이트 합니다.
	 * 
	 * @param v 볼륨
	 */
	private void updateVolume(float v) {
		
		/*
		 * 게임 내 preference 업데이트
		 */
		game.getPreferencesManager().setVolume(v);
		
		/*
		 * 옵션 화면 내 텍스트 업데이트
		 */
		float volume = v * 100;
		this.volumeText.setText(String.format(Locale.KOREAN, "%1.0f%%", volume));
		
	}//end of updateVolumeLabel()

}
//end of OptionsScreen.java